﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace SoufunData
{
    public partial class LandStatisticsRankingsForm : DevExpress.XtraEditors.XtraForm
    {
        public LandStatisticsRankingsForm()
        {
            InitializeComponent();
        }

        private void LandInfoListForm_Load(object sender, EventArgs e)
        {
            dateEdit_CtimeStart.DateTime = DateTime.Now.AddYears(-3);
            dateEdit_CtimeEnd.DateTime = DateTime.Now;



            gridControl2.DataSource = null;
            gridView2.GroupPanelText = "正在统计中...";

      
            Thread t = new Thread(LoatData) { IsBackground = true };
            t.Start();
        }
          
        private readonly SqlClass _sqlClass = new SqlClass();
 

        private void LoatData()
        {
            StringBuilder strWhere = new StringBuilder();
        
            strWhere.Append("TransactionTime between '");
            strWhere.Append(dateEdit_CtimeStart.DateTime);
            strWhere.Append("' and '");
            strWhere.Append(dateEdit_CtimeEnd.DateTime);
            strWhere.Append("' ");
             
            int topValue = int.Parse(radioGroup1.Properties.Items[radioGroup1.SelectedIndex].Value.ToString());
            string strOrderPara =
                imgComBox_OrderPara.Properties.Items[imgComBox_OrderPara.SelectedIndex].Value.ToString();
            string strGroupPara =
                imgComBox_GroupPara.Properties.Items[imgComBox_GroupPara.SelectedIndex].Value.ToString();
            try
            {
                DataSet ds =_sqlClass.GetLandStatisticsRankings(topValue,strWhere.ToString(),strOrderPara,strGroupPara);
                if (ds==null || ds.Tables.Count==0)
                {
                    XtraMessageBox.Show("请检查数据库连接是否正常！");
                }
                else
                {
                    ds.Tables[0].Columns.Add("Ranking");
                    int ranking = 1;
                    float preValue = 0;
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        float currentValue = float.Parse(ds.Tables[0].Rows[i][strOrderPara].ToString());
                        if (i == 0)
                        { 
                            ds.Tables[0].Rows[i]["Ranking"] = 1;
                        }
                        else
                        {
                            if (currentValue < preValue)
                            {                              
                                ranking++;
                            }
                            ds.Tables[0].Rows[i]["Ranking"] = ranking;                            
                        }
                        preValue = currentValue; 
                    }
                    BeginInvoke(new MethodInvoker(() =>
                    {                        
                        gridControl2.DataSource = ds.Tables[0];
                        switch (strGroupPara)
                        {
                            case "`Group`":
                                gridView2.Columns["Group"].Visible = true;
                                gridView2.Columns["AdministratiVeregion"].Visible = false;
                                gridView2.Columns["Board"].Visible = false;
                                gridView2.Columns["PointsLoop"].Visible = false;                              
                                break;
                            case "AdministratiVeregion":
                                gridView2.Columns["Group"].Visible = false;
                                gridView2.Columns["AdministratiVeregion"].Visible = true;
                                gridView2.Columns["Board"].Visible = false;
                                gridView2.Columns["PointsLoop"].Visible = false;    
                                break;
                            case "Board":
                                 gridView2.Columns["Group"].Visible = false;
                                 gridView2.Columns["AdministratiVeregion"].Visible = false;
                                gridView2.Columns["Board"].Visible = true;
                                gridView2.Columns["PointsLoop"].Visible = false;   
                                break;
                            case "PointsLoop":
                                  gridView2.Columns["Group"].Visible = false;
                                 gridView2.Columns["AdministratiVeregion"].Visible = false;
                                gridView2.Columns["Board"].Visible = false;
                                gridView2.Columns["PointsLoop"].Visible = true;   
                                break;
                           
                        }
                        gridView2.GroupPanelText = "统计结果";
                    }));
                }
                BeginInvoke(new MethodInvoker(() =>
                    {                     
                        btn_select.Enabled = true;
                    }));


            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message);
                BeginInvoke(new MethodInvoker(() =>
                {
                    btn_select.Enabled = true;
                }));
            }
        }

        
        private void btn_select_Click(object sender, EventArgs e)
        {
            if (!CheckDateEdit())
            {
                return;
            }
            gridControl2.DataSource = null;
            gridView2.GroupPanelText = "正在查询...";
     
            btn_select.Enabled = false;
            Thread t = new Thread(LoatData) { IsBackground = true };
            t.Start();
        }
 
        private bool CheckDateEdit()
        {

            if (dateEdit_CtimeStart.DateTime > DateTime.Parse("1000/01/01")
                && dateEdit_CtimeEnd.DateTime > DateTime.Parse("1000/01/01")
                && dateEdit_CtimeStart.DateTime > dateEdit_CtimeEnd.DateTime)
            {
                XtraMessageBox.Show("您选择的【成交时间】范围中的起始日期不能大于截止日期！");
                dateEdit_CtimeStart.Focus();
                return false;
            }
           
            return true;
        }



        private void btn_toExcel1_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                return;
            }
            string fileName = "地块汇总排行榜";
            saveFileDialog1.FileName = fileName;
            saveFileDialog1.Filter = "Excel文件(*.xls)|*.xls";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
             
               gridView2.ExportToXls(saveFileDialog1.FileName);    
              
                XtraMessageBox.Show("Excel导出完成！");
            }
        }

 

    }
}